package com.ruyuan.eshop.data.migrate.tool.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruyuan.eshop.data.migrate.tool.mapper.targetdb.TargetOrderAmountDetailMapper;
import com.ruyuan.eshop.data.migrate.tool.model.order.OrderAmountDetailDO;
import com.ruyuan.eshop.data.migrate.tool.service.TargetOrderAmountDetailService;
import com.ruyuan.eshop.data.migrate.tool.util.LoggerFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author zhonghuashishan
 * @version 1.0
 */
@Slf4j
@Service
public class TargetOrderAmountDetailServiceImpl implements TargetOrderAmountDetailService {

    @Autowired
    private TargetOrderAmountDetailMapper targetOrderAmountDetailMapper;

    @Override
    public Long getMaxId() {
        return targetOrderAmountDetailMapper.getMaxId();
    }

    @Override
    @Transactional(value = "TargetTransactionManager")
    public void consumer(CanalEntry.EventType eventType, JSONObject beforeData, JSONObject afterData) {

        log.info(LoggerFormat.build()
                .remark("consumer->request")
                .data("eventType", eventType)
                .data("beforeData", beforeData)
                .data("afterData", afterData)
                .finish());

        if(CanalEntry.EventType.INSERT.equals(eventType) || CanalEntry.EventType.UPDATE.equals(eventType)) {
            OrderAmountDetailDO orderAmountDetailDO = afterData.toJavaObject(OrderAmountDetailDO.class);

            // 新增或更新记录
            insertOrUpdate(orderAmountDetailDO.getId(), orderAmountDetailDO);
            return;
        }

        if(CanalEntry.EventType.DELETE.equals(eventType)) {
            OrderAmountDetailDO orderAmountDetailDO = beforeData.toJavaObject(OrderAmountDetailDO.class);

            // 删除记录
            delete(orderAmountDetailDO);
        }
    }

    @Override
    @Transactional(value = "TargetTransactionManager")
    public void batchInsert(List<OrderAmountDetailDO> orderAmountDetailDOList) {

        log.info(LoggerFormat.build()
                .remark("sync->request")
                .data("orderAmountDetailDOList", orderAmountDetailDOList)
                .finish());

        targetOrderAmountDetailMapper.batchInsert(orderAmountDetailDOList);
    }

    /**
     * 新增操作 或者 更新操作
     * @param orderAmountDetailDO 售后单日志
     */
    private void insertOrUpdate(Long id, OrderAmountDetailDO orderAmountDetailDO) {
        // 先验证一下是否存在历史数据
        Date gmtModified = targetOrderAmountDetailMapper.getGmtModified(id, orderAmountDetailDO.getOrderId());
        if(gmtModified == null) {
            // 不存在则执行新增操作
            targetOrderAmountDetailMapper.insert(orderAmountDetailDO);
            return;
        }

        // 已经存在记录了，比较一下gmtModified
        if(orderAmountDetailDO.getGmtModified().after(gmtModified)) {
            // 如果当前的记录比较新，则执行更新操作
            LambdaUpdateWrapper<OrderAmountDetailDO> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(OrderAmountDetailDO::getId, orderAmountDetailDO.getId());
            updateWrapper.eq(OrderAmountDetailDO::getOrderId, orderAmountDetailDO.getOrderId());
            targetOrderAmountDetailMapper.update(orderAmountDetailDO, updateWrapper);
        }

        // 否则，丢弃记录不处理
    }

    /**
     * 删除操作
     * @param orderAmountDetailDO 订单费用明细
     */
    private void delete(OrderAmountDetailDO orderAmountDetailDO) {
        LambdaQueryWrapper<OrderAmountDetailDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderAmountDetailDO::getId, orderAmountDetailDO.getId());
        queryWrapper.eq(OrderAmountDetailDO::getOrderId, orderAmountDetailDO.getOrderId());
        targetOrderAmountDetailMapper.delete(queryWrapper);
    }


}